"""
公共用法接口
"""
import os
from conf import setting
from db import models

#查看校区
def select_school():
    shcool_path =os.path.join(setting.DATA_PATH,"School")
    if not os.path.exists(shcool_path):
        return False,"未创建此校区，请联系管理员！"
    school_list = os.listdir(shcool_path)
    return True,school_list

#查看学生校区课程
def select_crouse(student_name):
    student_obj = models.Student.self_select(student_name)
    if not student_obj.school:
        return False,"未选择学校，请先选择学校!"
    school_name = student_obj.school
    school_obj = models.School.self_select(school_name) 
    if not school_obj:
        return False,"此学校不存在，请联系管理员！"
    crouse_list = school_obj.crouse
    return True,crouse_list


#查看学校课程
def select_school_crouse(school_name):
    shcool_obj = models.School.self_select(school_name)
    if not shcool_obj:
        return False,"没有此学校请练习管理员!"
    shcool_list = shcool_obj.crouse
    return True,shcool_list





#登录功能
def login(name,password,type):
    #判断用户是否存在
    #先判断是那个类型用户登录
    if type == "admin":
        obj = models.Admin.self_select(name)
    elif type == "student":
        obj = models.Student.self_select(name)
    elif type == "teather":
        obj = models.Teather.self_select(name)        
    else:
        return False,"用户类型不存在！"
        
    # print(obj)

    if not obj:
        return False,"用户不存在！"

    #判断密码是否是相同
    if password == obj.password:
        return True,"用户登录成功!"
    else:
        return False,"用户密码错误!"